package shopping.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import shopping.domain.User;
import shopping.utils.DataSourceUtil;

import java.sql.SQLException;

public class UserDao {
    public void save(User user) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "insert into user values(?,?,?,?,?,?,?,?)";
        try {
            qr.update(sql,
                    user.getUid(), user.getUsername(), user.getPassword(),
                    user.getName(), user.getEmail(), user.getBirthday(),
                    user.getGender(), user.getRemark()
            );
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    public User findUserByUAP(String username, String password) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select * from user where username=? and password=?";
        try {
            User user = qr.query(sql, new BeanHandler<>(User.class),username,password);
            return user;
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }
}
